package cl.agrosuper.dao
{
	import cl.agrosuper.to.Plantilla;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;

	public class PlantillaDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		private var _grupoItemDB:GrupoItemDB;
		private var _plantillaActual:Plantilla;
		
		public function PlantillaDB(conn:SQLConnection)
		{
			if(conn != null){
				this.conn = conn;
				statement = new SQLStatement();
				statement.sqlConnection = conn;
			}
			else{
				DBManager.conectar(resultConectarDB);
			}
		}
		
		private function resultConectarDB():void{
			this.conn = DBManager.connection;
			statement = new SQLStatement();
			statement.sqlConnection = conn;
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		//-------------------------------------------------
		
		public function obtenerPlantillaActual():Plantilla{					
			statement.text = "SELECT id, fecha " +
							 "FROM plantilla_auditoria " +
							 "WHERE actual = true";
			
			statement.execute();
			
			var result:SQLResult = statement.getResult();
			if (result != null && result.data != null && result.data.length > 0) {  
				this._plantillaActual = new Plantilla();
				this._plantillaActual.id = result.data[0].ID;
				this._plantillaActual.fecha = result.data[0].FECHA;
				
				this._grupoItemDB = new GrupoItemDB(this.conn);
				this._plantillaActual.gruposItem = this._grupoItemDB.obtenerGruposItemPlantilla(this._plantillaActual.id);
				
				return this._plantillaActual;
			}
			
			return new Plantilla();
		}
		
		//------------------------------------------------
		
		public function obtenerPlantillaActualASync():void{					
			statement.text = "SELECT id, fecha " +
				"FROM plantilla_auditoria " +
				"WHERE actual = true";
			statement.addEventListener(SQLEvent.RESULT, resultObtenerPlantillaActual);
			statement.execute();
		}
		
		private function resultObtenerPlantillaActual(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerPlantillaActual);
			var result:SQLResult = statement.getResult();
			if (result != null && result.data != null && result.data.length > 0) {  
				this._plantillaActual = new Plantilla();
				this._plantillaActual.id = result.data[0].ID;
				this._plantillaActual.fecha = result.data[0].FECHA;
				
				this._grupoItemDB = new GrupoItemDB(this.conn);
				this._grupoItemDB.callback = resultObtenerGruposItemPlantilla;
				this._grupoItemDB.obtenerGruposItemPlantillaAsync(this._plantillaActual.id);
			}
		}
		
		private function resultObtenerGruposItemPlantilla(arrayGrupos:ArrayCollection):void{
			if(this._callback != null){
				this._plantillaActual.gruposItem = arrayGrupos;
				this._callback(this._plantillaActual);
			}
		}
	}
}